package com.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.domain.entity.Goods;
import com.domain.entity.Picture;
import com.domain.req.AddPictureReq;
import com.domain.req.UpdatePictureReq;
import com.exception.ServiceException;
import com.mapper.GoodsMapper;
import com.mapper.PictureMapper;
import com.service.PictureService;
import com.utils.ValidatorUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author caishenao
 * @description 针对表【picture】的数据库操作Service实现
 * @createDate 2023-10-12 20:12:49
 */
@Service
@Slf4j
public class PictureServiceImpl implements PictureService {

    @Resource
    private PictureMapper pictureMapper;

    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 查询所有轮播图信息
     *
     * @return 所有轮播图信息
     */
    @Override
    public List<Picture> list() {
        log.info("请求list查询所有轮播图信息");
        List<Picture> pictureList = pictureMapper.list();

        // 补充商品信息
        for (Picture picture : pictureList) {
            Integer goodsId = picture.getGoodsId();
            Goods goods = goodsMapper.getById(goodsId);
            picture.setGoods(goods);
        }
        log.info("请求list查询所有轮播图信息的出参：{}", JSONObject.toJSONString(pictureList));
        return pictureList;
    }

    /**
     * 添加轮播图信息
     *
     * @param addPictureReq 新增轮播图请求
     */
    @Override
    public void addPicture(AddPictureReq addPictureReq) {
        log.info("添加轮播图信息氢气入参:{}", JSONObject.toJSONString(addPictureReq));
        // 1. 注解校验
        ValidatorUtils.validateEntity(addPictureReq);

        // 2. 判断商品id是否存在
        Integer goodsId = addPictureReq.getGoodsId();
        Goods goods = goodsMapper.getById(goodsId);
        if (goods == null) {
            // 商品信息不存在
            log.error("id为：{}的商品信息不存在!", JSONObject.toJSONString(goodsId));
            throw new ServiceException("商品信息不存在!");
        }

        // 3. 转换为对应实体对象
        Picture picture = addPictureReqConversion(addPictureReq);

        // 4. 新增到数据库中
        pictureMapper.insert(picture);
    }

    /**
     * 根据id删除轮播图信息
     *
     * @param id 轮播图id
     */
    @Override
    public void deleteById(Integer id) {
        log.info("根据id删除轮播图信息请求入参:{}", JSONObject.toJSONString(id));
        // 1. 查询id是否存在
        Picture picture = pictureMapper.getById(id);
        if (picture == null) {
            log.error("id为:{}的轮播图信息不存在!", JSONObject.toJSONString(id));
        }
        // 2. 根据id删除轮播图
        pictureMapper.deleteById(id);
    }

    /**
     * 根据轮播图id修改轮播图信息
     *
     * @param updatePictureReq 修改请求
     */
    @Override
    public void updateById(UpdatePictureReq updatePictureReq) {
        log.info("请求轮播图修改接口,请求入参:{}", JSONObject.toJSONString(updatePictureReq));
        // 1. 注解校验
        ValidatorUtils.validateEntity(updatePictureReq);

        // 2. 查询轮播图信息是否存在
        Picture picture = pictureMapper.getById(updatePictureReq.getId());
        if (picture == null) {
            // 轮播图不存在
            log.error("id为：{}的轮播图信息不存在！", JSONObject.toJSONString(updatePictureReq.getId()));
            throw new ServiceException("轮播图不存在!");
        }

        // 3. 查询商品信息
        Goods goods = goodsMapper.getById(updatePictureReq.getGoodsId());
        if (goods == null) {
            // 商品信息不存在
            log.error("id为:{}的商品信息不存在!", JSONObject.toJSONString(updatePictureReq.getGoodsId()));
            throw new ServiceException("商品信息不存在!");
        }

        // 4. 转换为实体对象
        Picture update = updatePictureReqConversion(updatePictureReq);

        // 5. 根据id修改
        pictureMapper.updateById(update);
    }

    /**
     * 根据id查询轮播图信息
     *
     * @param id 轮播图id
     * @return 轮播图信息
     */
    @Override
    public Picture getById(Integer id) {
        log.info("请求根据id查询轮播图信息，入参为:{}", JSONObject.toJSONString(id));
        Picture picture = pictureMapper.getById(id);
        log.info("请求根据id查询轮播图信息结束,出参为:{}", JSONObject.toJSONString(picture));
        return picture;
    }

    /**
     * 转换为图片实体对象
     *
     * @param updatePictureReq 修改请求
     * @return 轮播图实体对象
     */
    private Picture updatePictureReqConversion(UpdatePictureReq updatePictureReq) {
        Picture picture = new Picture();
        picture.setId(updatePictureReq.getId());
        picture.setName(updatePictureReq.getName());
        picture.setUrl(updatePictureReq.getUrl());
        picture.setDescription(updatePictureReq.getDescription());
        picture.setGoodsId(updatePictureReq.getGoodsId());
        return picture;
    }

    /**
     * 轮播图新增对象转换器
     *
     * @param addPictureReq 新增请求
     * @return 轮播图实体对象
     */
    private Picture addPictureReqConversion(AddPictureReq addPictureReq) {
        Picture picture = new Picture();
        picture.setGoodsId(addPictureReq.getGoodsId());
        picture.setName(addPictureReq.getName());
        picture.setDescription(addPictureReq.getDescription());
        picture.setUrl(addPictureReq.getUrl());
        picture.setCreateTime(new Date());
        return picture;
    }
}




